unit UPessoaModel;

interface

uses UIMVCinterfaces, Dialogs, Windows, SysUtils, DB, ADODB;

type
  IManterContato = interface(IModel)
    ['{96323491-90B6-4BE3-973F-F0A3725116EB}']
    procedure SearchName(const Value: String);
    procedure Insert;
    procedure Delete;
    procedure Update;
    procedure Save;
    procedure Cancel;
  end;

  TPessoaModel = class(TInterfacedObject, IManterContato)
    private
      FConn: TADOConnection;

      function GetConn: TADOConnection;
      procedure SetConn(const Value: TADOConnection);

    public
      procedure SearchName(const Value: String);
      procedure Insert;
      procedure Delete;
      procedure Update;
      procedure Save;
      procedure Cancel;
      procedure Initialize;

      constructor Create;
      destructor Destroy;
  end;

implementation


{ TPessoaModel }

procedure TPessoaModel.Cancel;
begin
  ShowMessage(TPessoaModel.ClassName + 'Cancel');
end;

constructor TPessoaModel.Create;
  var
    ADOConn: TADOConnection;
begin
  ADOConn := TADOConnection.Create(nil);
  ADOConn.ConnectionTimeout := 60000;
  ADOConn.CommandTimeout := 60000;
  ADOConn.LoginPrompt := False;
  ADOConn.Provider := 'SQLOLEDB.1';
  ADOConn.ConnectionString := 'Provider=SQLOLEDB.1;Password=123;Persist Security Info=True;User ID=sa;Initial Catalog=CRWEB_TESTE;Data Source=192.168.1.161';
  ADOConn.Connected := False;
  Self.SetConn(ADOConn);
end;

procedure TPessoaModel.Delete;
begin
  ShowMessage(TPessoaModel.ClassName + 'Delete');
end;

destructor TPessoaModel.Destroy;
begin
  if(Self.GetConn.Connected)then
  begin
    Self.GetConn.Connected := False;
    Self.GetConn.Destroy;
  end;
end;

function TPessoaModel.GetConn: TADOConnection;
begin
  Result := FConn;
end;

procedure TPessoaModel.Initialize;
begin
  ShowMessage(TPessoaModel.ClassName + 'Initialize');
end;

procedure TPessoaModel.Insert;
begin
  ShowMessage(TPessoaModel.ClassName + 'Insert');
end;

procedure TPessoaModel.Save;
begin
  ShowMessage(TPessoaModel.ClassName + 'Save');
end;

procedure TPessoaModel.SearchName(const Value: String);
  var
    ADOQryModel: TADOQuery;
begin
  if(Value = '')then
  begin
    ShowMessage('Voce deve preencher o campo.');
  end
  else
  begin
    try
      try
        if(Not Self.GetConn.Connected)then
        begin
          Self.GetConn.Connected := True;
        end;

        ADOQryModel := TADOQuery.Create(nil);
        ADOQryModel.Connection := Self.GetConn;
        ADOQryModel.Close;
        ADOQryModel.SQL.Text := 'SELECT * FROM usuario ';
        ADOQryModel.SQL.Add('WHERE usu_login LIKE ' + QuotedStr(Value));
        ADOQryModel.SQL.Add('ORDER BY usu_login');
        ADOQryModel.Open;

        if(ADOQryModel.RecordCount > 0)then
        begin
          ShowMessage('Class ' + ADOQryModel.ClassName + ' - ' + IntToStr(ADOQryModel.RecordCount));
        end;
      except
        on Erro: Exception do
        begin
          ShowMessage('Class: ' + Erro.ClassName + '' + Erro.Message);
        end;
      end; // fim try except
    finally
      ADOQryModel.Free;
    end; // fim try finally
  end;
end;

procedure TPessoaModel.SetConn(const Value: TADOConnection);
begin
  FConn := Value;
end;

procedure TPessoaModel.Update;
begin
  ShowMessage(TPessoaModel.ClassName + 'Update');
end;

end.
